package com.engine.update.cmd;

import com.engine.core.domain.GenTable;
import com.engine.core.gen.Mysql;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.rowMapper.JdbcTypeBiz;
import com.engine.core.rowMapper.MappedType;
import com.engine.core.util.JdbcTemplateUtils;
import com.engine.core.util.QueryUtil;
import com.engine.core.util.Response;
import com.engine.core.interceptor.ResponseCommand;
import com.engine.system.User;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;
import java.util.TreeMap;

public class UpdateOneCommand extends ResponseCommand {

    public UpdateOneCommand(User user, Map<String, Object> params) {
        super(user, params);
    }


    /**
     * @param commandContext
     * @return
     * @throws Exception
     */
    @Override
    public Response execute(JdbcCommandContext commandContext) throws Exception {
        Response response=Response.static_success("执行成功!");
        try {
            String tableName=(String)params.get(QueryUtil.QUERY_TABLE);
            JdbcTemplate jdbcTemplate=commandContext.getJdbcTemplate();
            Map<String,String> othersMap= JdbcTypeBiz.getUpdateOthersByParams(params);
            Map<String,Object> dataSave=JdbcTypeBiz.getQueryParamsMap(params,commandContext.getGlobal().getDateformat());
            TreeMap<String, MappedType> mappedTypeMap=JdbcTypeBiz.getOneParamsType(params,commandContext.getGlobal().getDateformat());
            GenTable genTable= Mysql.getTable(tableName,jdbcTemplate);
            response= JdbcTemplateUtils.updateOneDataByColumns(genTable,jdbcTemplate, dataSave,mappedTypeMap,othersMap);
        } catch (Exception e) {
            response.setErr(e);
        }
        return response;
    }
}
